﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 通讯录
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listBox.Items.Add("陈哲帆\t男\t17322971141\t广东省/揭阳市/榕城区\t19岁\t2003/07/20");
        }

        private PeoInfo fromStringtoPeoInfo(string Item)
        {
            string[] str = Item.Split('\t');
            string[] date = str[5].Split('/');

            PeoInfo peo = new PeoInfo(str[0], str[1], str[2], str[3], new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2])));
            return peo;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Text = "添加个人信息";
            if(form2.ShowDialog() == DialogResult.OK)
            {
                PeoInfo peo = form2.PeoInfo;
                listBox.Items.Add(peo.getInfo());
            }

        }

        private void delButton_Click(object sender, EventArgs e)
        {
            try
            {
                listBox.Items.RemoveAt(listBox.SelectedIndex);
            }
            catch(ArgumentOutOfRangeException)
            {
                MessageBox.Show("未选择项目\n" , "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            Form3 form3 = new Form3();
            if(form3.ShowDialog() == DialogResult.OK)
            {
                string str = form3.getString;
                int flag = form3.getFlag;
                string find = "";

                for(int i = 0; i<listBox.Items.Count; i++)
                {
                    PeoInfo peo = fromStringtoPeoInfo(listBox.Items[i].ToString());

                    if(flag == 1)
                    {
                        if(peo.getName.Contains(str))
                        {
                            find += listBox.Items[i].ToString();
                            find += "\n";
                        }
                    }
                    else if(flag == 2)
                    {
                        if(peo.getSex == str)
                        {
                            find += listBox.Items[i].ToString();
                            find += "\n";
                        }
                    }
                    else if(flag == 3)
                    {
                        if(peo.getTele.Contains(str))
                        {
                            find += listBox.Items[i].ToString();
                            find += "\n";
                        }
                    }
                    else if(flag == 4)
                    {
                        if(peo.getAddr == str)
                        {
                            find += listBox.Items[i].ToString();
                            find += "\n";
                        }
                    }
                    else if(flag == 5)
                    {
                        if(peo.getAge.ToString() == str)
                        {
                            find += listBox.Items[i].ToString();
                            find += "\n";
                        }
                    }
                    else if (flag == 6)
                    {
                        if(peo.getBirth == str)
                        {
                            find += listBox.Items[i].ToString();
                            find += "\n";
                        }
                    }
                }

                if(find != "")
                {
                    MessageBox.Show(find, "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("无匹配项", "查找结果", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        private void modifyButton_Click(object sender, EventArgs e)
        {
            try
            {
                string item = listBox.SelectedItem.ToString();
                int index = listBox.SelectedIndex;
                PeoInfo Peo = fromStringtoPeoInfo(item);
                Form2 f2 = new Form2(Peo);
                f2.Text = "修改个人信息";

                if (f2.ShowDialog() == DialogResult.OK)
                {
                    listBox.Items.RemoveAt(index);
                    PeoInfo peo = f2.PeoInfo;
                    listBox.Items.Insert(index, peo.getInfo());
                }
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("未选择项目\n" , "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void sortButton_Click(object sender, EventArgs e)
        {
            int sortkey = 0;
            int flag = -1;

            Form4 f4 = new Form4();
            if( f4.ShowDialog() == DialogResult.OK )
            {
                sortkey = f4.getSortKey;
                flag = f4.getFlag;
            }

            List<PeoInfo> list = new List<PeoInfo>();
            
            int n = listBox.Items.Count;

            for (int i = 0; i < n; i++)
            {
                string Item = listBox.Items[i].ToString();
                PeoInfo item = fromStringtoPeoInfo(Item);
                list.Add(item);
            }
            
            if(sortkey == 1)
            {
                if (flag == 0)
                {
                    list = list.OrderByDescending(i => i.getName).ToList();
                }
                else if(flag == 1)
                {
                    list = list.OrderBy(i => i.getName).ToList();
                }
            }
            else if(sortkey == 2)
            {
                if (flag == 0)
                {
                    list = list.OrderByDescending(i => i.getSex).ToList();
                }
                else if (flag == 1)
                {
                    list = list.OrderBy(i => i.getSex).ToList();
                }
            }
            else if (sortkey == 3)
            {
                if (flag == 0)
                {
                    list = list.OrderByDescending(i => i.getTele).ToList();
                }
                else if (flag == 1)
                {
                    list = list.OrderBy(i => i.getTele).ToList();
                }
            }
            else if (sortkey == 4)
            {
                if (flag == 0)
                {
                    list = list.OrderByDescending(i => i.getAddr).ToList();
                }
                else if (flag == 1)
                {
                    list = list.OrderBy(i => i.getAddr).ToList();
                }
            }
            else if (sortkey == 5)
            {
                if (flag == 0)
                {
                    list = list.OrderByDescending(i => i.getAge).ToList();
                }
                else if (flag == 1)
                {
                    list = list.OrderBy(i => i.getAge).ToList();
                }
            }
            else if (sortkey == 6)
            {
                if (flag == 0)
                {
                    list = list.OrderByDescending(i => i.getBirth).ToList();
                }
                else if (flag == 1)
                {
                    list = list.OrderBy(i => i.getBirth).ToList();
                }
            }

            listBox.Items.Clear();

            foreach (PeoInfo item in list)
            {
                listBox.Items.Add(item.getInfo());
            }
        }

        private void showButton_Click(object sender, EventArgs e)
        {
            try
            {
                string[] str = listBox.SelectedItem.ToString().Split('\t');
                string message = "姓名:" + str[0] + "\n" + "性别：" + str[1] + "\n" + "手机电话：" + str[2] + "\n" + "地址：" + str[3] + "\n" + "年龄：" + str[4] + "\n" + "出生日期：" + str[5];
                MessageBox.Show(message, "个人详细信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(NullReferenceException)
            {
                MessageBox.Show("未选择项目\n" , "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            Form5 form5 = new Form5();
            int flag = -1;
            string path = "";

            if(form5.ShowDialog() == DialogResult.OK)
            {
                flag = form5.getFlag;
            }

            if(flag == 0)
            {
                path = Directory.GetCurrentDirectory() + "/通讯录.txt";

                StreamWriter st = new StreamWriter(path, false, System.Text.Encoding.Default);

                for (int i = 0; i < listBox.Items.Count; i++)
                {
                    st.WriteLine(listBox.Items[i].ToString());
                }
                st.Close();

                MessageBox.Show("保存成功\n保存路径：" + path, "保存文件", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            else if(flag == 1)
            {
                saveFileDialog.DefaultExt = "*.txt";
                saveFileDialog.Filter = "|*.txt";
                
                if(saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    path = saveFileDialog.FileName;

                    StreamWriter st = new StreamWriter(path, false, System.Text.Encoding.Default);

                    for (int i = 0; i < listBox.Items.Count; i++)
                    {
                        st.WriteLine(listBox.Items[i].ToString());
                    }
                    st.Close();

                    MessageBox.Show("保存成功\n保存路径：" + path, "保存文件", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void laodButton_Click(object sender, EventArgs e)
        {
            Form6 form6 = new Form6();
            int flag = -1;
            string path = "";

            if (form6.ShowDialog() == DialogResult.OK)
            {
                flag = form6.getFlag;
            }

            try
            {
                if (flag == 0)
                {
                    path = Directory.GetCurrentDirectory() + "/通讯录.txt";

                    StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);

                    listBox.Items.Clear();
                    string str = sr.ReadLine();
                    while(str != null)
                    {
                        listBox.Items.Add(str);
                        str = sr.ReadLine();
                    }
                    sr.Close();

                    MessageBox.Show("加载文件成功\n文件路径：" + path, "加载文件", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else if( flag == 1)
                {
                    openFileDialog.DefaultExt = "*.txt";
                    openFileDialog.Filter = "|*.txt";

                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        path = openFileDialog.FileName;

                        StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);

                        listBox.Items.Clear();
                        string str = sr.ReadLine();
                        while (str != null)
                        {
                            listBox.Items.Add(str);
                            str = sr.ReadLine();
                        }
                        sr.Close();

                        MessageBox.Show("加载文件成功\n文件路径：" + path, "加载文件", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch(FileNotFoundException)
            {
                MessageBox.Show("文件不存在", "加载文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            delButton_Click(null, null);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            modifyButton_Click(null, null);
        }

        private void 详情ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showButton_Click(null, null);
        }
    }
}
